package com.xgpro.common.results;

import com.xgpro.common.enums.ResultCodeEnum;

import java.io.Serializable;
/**
 * @author tx
 * @version 1.0
 * @date 2019/8/24 12:58
 */
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = -1;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 返回的消息
     */
    private String resultMsg;

    /**
     * 返回的信息编号
     */
    private String resultCode;

    /**
     * 返回值，类型为T
     */
    private T data;

    public ResponseResult() {
    }

    public ResponseResult(boolean success, String resultMsg, String resultCode, T data) {
        this.success = success;
        this.resultMsg = resultMsg;
        this.resultCode = resultCode;
        this.data = data;
    }

    public static ResponseResult ok(Object obj){
        ResponseResult result = new ResponseResult();
        result.setData(obj);
        result.setResultCode(ResultCodeEnum.SUCCESS.getCode());
        result.setResultMsg(ResultCodeEnum.SUCCESS.getMessage());
        result.setSuccess(true);
        return result;
    }

    public static ResponseResult ok(){
        ResponseResult result = new ResponseResult();
        result.setSuccess(true);
        result.setResultCode(ResultCodeEnum.SUCCESS.getCode());
        result.setResultMsg(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    public static ResponseResult failure(String resultCode, String resultMsg){
        ResponseResult result = new ResponseResult();
        result.setSuccess(false);
        result.setResultCode(resultCode);
        result.setResultMsg(resultMsg);
        return result;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "success=" + success +
                ", resultMsg='" + resultMsg + '\'' +
                ", resultCode='" + resultCode + '\'' +
                ", data=" + data +
                '}';
    }
}
